package com.longge;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;

public class MaximizeSquareArea {

    public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) {

        ArrayList<Integer> hFencesList = new ArrayList<>();
        ArrayList<Integer> vFencesList = new ArrayList<>();

        hFencesList.add(1);
        hFencesList.add(m);
        vFencesList.add(1);
        vFencesList.add(n);

        for (int hFence : hFences) {
            hFencesList.add(hFence);
        }
        for (int vFence : vFences) {
            vFencesList.add(vFence);
        }

        hFencesList.sort(Comparator.comparingInt(i->i));
        vFencesList.sort(Comparator.comparingInt(i->i));

        HashSet<Integer> xSet = new HashSet<>();
        HashSet<Integer> ySet = new HashSet<>();


        for (int i = 0; i < hFencesList.size(); i++) {
            for (int j = i + 1; j < hFencesList.size(); j++) {
                xSet.add(hFencesList.get(j) - hFencesList.get(i));
            }
        }
        for (int i = 0; i < vFencesList.size(); i++) {
            for (int j = i + 1; j < vFencesList.size(); j++) {
                ySet.add(vFencesList.get(j) - vFencesList.get(i));
            }
        }


        int ans = -1;
        for (Integer x : xSet) {
            if (ySet.contains(x)) {
                ans = Math.max(ans, (int) (Math.pow(x, 2) % (Math.pow(10,9)+7)));
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        new MaximizeSquareArea().maximizeSquareArea(3,9,new int[]{2},new int[]{8,6,5,3});
    }

}
